package com.ruoyi.job.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.job.domain.DataCustomerInfo;
import com.ruoyi.job.mapper.DataCustomerInfoMapper;
import com.ruoyi.job.util.BASE64DecodedMultipartFile;
import com.ruoyi.job.util.IdCardNetVerify;
import com.ruoyi.job.util.IdcardValidator;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 获取身份证联网核查结果
 *
 * @author sxy
 */
@Component("idCardNwtVerifyTask")
public class IdCardNwtVerifyTask {
    private static Logger logger = LoggerFactory.getLogger(IdCardNwtVerifyTask.class);

    @Resource
    private RemoteFileService remoteFileService;

    @Resource
    private DataCustomerInfoMapper dataCustomerInfoMapper;

    private IdcardValidator idcardValidator;

    public void doMethod() {
        logger.info("==========获取联网核查图片开始==============");
        idcardValidator = new IdcardValidator();
        //只取18位身份证，15位身份证情况太复杂
        List<DataCustomerInfo> dataCustomerInfos = dataCustomerInfoMapper.selectList();
        logger.info("==========联网核查待查客户数量==============》{}",dataCustomerInfos.size());
        for (int i = 0; i < dataCustomerInfos.size(); i++) {
            /**
             * 通过正则表达式判断身份证是否正确
             * 1.身份证号码正确则正常查询联网核查
             * 2.身份证号码异常则直接更新联网核查描述信息
             */
            DataCustomerInfo dataCustomerInfo = dataCustomerInfos.get(i);
            logger.info("当前客户信息===>{}",dataCustomerInfo.getIdCard());
            boolean isVaild = idcardValidator.isValidatedAllIdcard(dataCustomerInfo.getIdCard());
            if (isVaild) {
                String jsonStr = IdCardNetVerify.verify(dataCustomerInfo.getIdCard(), dataCustomerInfo.getName());
                if (StringUtils.isNotEmpty(jsonStr)) {
                    JSONObject json = JSON.parseObject(jsonStr);
                    String code = json.getString("code");
                    logger.info("==========联网核查返回code==============》{}",code);
                    if ("00".equals(code)) {
                        String msg = json.getString("msg");
                        logger.info("==========联网核查返回msg==============》{}",msg);
                        String photo = json.getString("photo");
                        
                        InputStream inputStream = BASE64DecodedMultipartFile.base64ToMultipartOnt(photo);
                        MultipartFile multipartFile = null;
                        try {
                            multipartFile = new MockMultipartFile(dataCustomerInfo.getIdCard() + ".jpg",
                                    dataCustomerInfo.getIdCard() + ".jpg",
                                    "multipart/form-data", inputStream);
                        } catch (IOException e) {
                            logger.info("==========联网核查图片转换异常==============》{}",e.getMessage());
                            dataCustomerInfo.setNetIdCardMsg(e.getMessage());
                            dataCustomerInfoMapper.updateDataCustomerInfoByIdCard(dataCustomerInfo);
                        }
                        R<SysFile> result = remoteFileService.uploadIdcardPhoto(multipartFile);
                        logger.info("==========联网核查图片上传结果==============》{}",result.getData());
                        dataCustomerInfo.setNetIdCardUrl(result.getData().getUrl());
                        dataCustomerInfo.setNetIdCardMsg(msg);
                        dataCustomerInfoMapper.updateDataCustomerInfoByIdCard(dataCustomerInfo);
                    } else {
                        logger.info("==========联网核查返回msg==============》{}",json.getString("msg"));
                        dataCustomerInfo.setNetIdCardMsg(json.getString("msg"));
                        dataCustomerInfoMapper.updateDataCustomerInfoByIdCard(dataCustomerInfo);
                    }
                }
            } else {
                //更新联网核查描述
                dataCustomerInfo.setNetIdCardMsg("身份证号码不合法！");
                dataCustomerInfoMapper.updateDataCustomerInfoByIdCard(dataCustomerInfo);
            }
        }

        logger.info("==========获取联网核查图片结束==============");
    }

}
